local skel = fk.CreateSkill {
  name = "emo__taoni",
  tags = {Skill.Permanent},
}

Fk:loadTranslationTable{
  ["emo__taoni"] = "讨逆",
  [":emo__taoni"] = "持恒技，出牌阶段限一次，你可选择一名角色，将该角色所有【杀】置于处理区，按逆时针顺序从其下家开始每名角色获得一张直到没有牌，然后你对最后一名获得牌的角色造成X点伤害（X为以此法分发的牌数）。若其未因此死亡，你可以失去Y点体力，令本阶段〖讨逆〗发动技能次数+1（Y为你本阶段已发动次数）。",
  ["#TaoniNoSlash"] = "%from 手牌中没有【杀】",

  ["#emo__taoni"] = "讨逆：选择一名角色，将其所有【杀】按逆时针分发，你对最后获得的角色造成伤害！",
  ["#emo__taoni-losehp"] = "讨逆：你可以失去 %arg 点体力，令〖讨逆〗发动技能次数+1",
}

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = "#emo__taoni",
  times = function (self, player)
    return (1 + player:getMark("emo__taoni-phase")) - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 1 + player:getMark("emo__taoni-phase")
  end,
  card_filter = Util.FalseFunc,
  card_num = 0,
  target_filter = function (self, player, to, selected)
    if #selected == 0 then
      return not to:isKongcheng() and to:getNextAlive() ~= to
    end
  end,
  target_num = 1,
  on_use = function(self, room, effect)
    local player = effect.from
    local from = effect.tos[1]
    local cards = table.filter(from:getCardIds(Player.Hand), function (id)
      return Fk:getCardById(id).trueName == "slash"
    end)
    if #cards == 0 then
      room:sendLog{ type = "#TaoniNoSlash", from = from.id, toast = true }
    else
      room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
      room:sendFootnote(cards, {
        type = "##ShowCard",
        from = from.id,
      })
      room:delay(1200)
      local to = from
      for i, id in ipairs(cards) do
        if room:getCardArea(id) == Card.Processing then
          room:doIndicate(to, {to:getNextAlive()})
          to = to:getNextAlive()
          room:delay(i < 10 and 400 or 100)
          room:obtainCard(to, id, true, fk.ReasonJustMove, to, skel.name)
        end
      end
      room:delay(800)
      room:doIndicate(player, {to})
      room:damage{
        from = player,
        to = to,
        damage = #cards,
        skillName = skel.name,
      }
      if to.dead then return end
    end
    if not player.dead and player.phase == Player.Play then
      local y = player:usedSkillTimes(skel.name, Player.HistoryPhase)
      if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__taoni-losehp:::"..y}) then
        room:addPlayerMark(player, "emo__taoni-phase")
        room:loseHp(player, y, skel.name)
      end
    end
  end,
})

return skel
